//
//  LocationView.m
//  RealatorView
//
//  Created by Ricky Jones on 1/25/09.
//  Copyright 2009 Vaporsoftware. All rights reserved.
//

#import "LocationViewController.h"
#import "Location.h";
#import "DataAccess.h";
#import "DBFoundation.h";
#import "RealatorViewAppDelegate.h";
#import "GetImageViewController.h";
#import "MapViewController.h";

@implementation LocationViewController

@synthesize CurrentLocation = _location;

@synthesize SaveButton = _saveButton;
@synthesize Address1TextBox = _address1TextBox;
@synthesize NameTextBox = _nameTextBox;
@synthesize Address1TextBox = _address1TextBox;
@synthesize Address2TextBox = _address2TextBox;
@synthesize CityTextBox = _cityTextBox;
@synthesize StateTextBox = _stateTextBox;
@synthesize ZipTextBox = _zipTextBox;
@synthesize ImageView = _imageView;
@synthesize ViewOnMapButton = _viewOnMapButton;

- (void)viewDidAppear:(BOOL)animated
{
	DataAccess* access = [[DataAccess alloc] init];
	_location = [access GetLocation:[_location LocationID]];
	[_imageView setAnimationImages:[_location Images]];
	[_imageView setAnimationDuration: [_location.Images count]];
	[_imageView startAnimating];
	
	if([_location myLocation].latitude == 0)
	{
		[_viewOnMapButton setEnabled:NO];
	}
	
	_locationManager = [[CLLocationManager alloc] init];
	[_locationManager setDelegate: self];
	
	_appDelegate = (RealatorViewAppDelegate *)[[UIApplication sharedApplication] delegate];	
}

-(void) LoadLocation:(Location*) locationToLoad
{
	[_nameTextBox setText:[_location Name]];
	[_address1TextBox setText:[_location Address1]];
	[_address2TextBox setText:[_location Address2]];
	[_cityTextBox setText:[_location City]];
	[_stateTextBox setText:[_location State]];
	[_zipTextBox setText:[_location Zip]];
	if([[_location Images] count] > 0)
		[_imageView setImage:[[_location Images] objectAtIndex:0]];
}

-(Location*)GetDisplayedLocation
{
	
	[_location setName:[_nameTextBox text]];
	[_location setAddress1:[_address1TextBox text]];
	[_location setAddress2:[_address2TextBox text]];
	[_location setCity:[_cityTextBox text]];
	[_location setState:[_stateTextBox text]];	
	[_location setZip:[_zipTextBox text]];	
	
	return _location;
}



- (void)viewDidLoad
{
		if(_location != nil)
		{
			[self LoadLocation:_location];
		}
}


- (IBAction)selectImage:(id)sender
{
		[_imageView stopAnimating];
		RealatorViewAppDelegate *appDelegate = (RealatorViewAppDelegate *)[[UIApplication sharedApplication] delegate];	
		_imagePicker = [[UIImagePickerController alloc] init];
		_imagePicker.delegate = self;
		_imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
		[[appDelegate window] addSubview:_imagePicker.view];
		
		UIImageView* imageView = [[UIImageView alloc] initWithFrame:[[appDelegate window] bounds]];
		imageView.hidden = YES;
		[[appDelegate window] addSubview:imageView];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
	LocationImage* img = [[LocationImage alloc] initWithCGImage:[image CGImage]];
	[_location AddImage:img];
	[_imageView setAnimationImages:_location.Images];
	[_imageView setAnimationDuration: [_location.Images count]];
	[_imageView startAnimating];
	[_imagePicker release];
}


- (IBAction)SetLocation_ButtonPressed:(id) sender
{
	[_locationManager startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{

	[_location setMyLocation:[newLocation coordinate]];
	[_viewOnMapButton setEnabled:YES];
}


-(IBAction)ViewOnMap_ButtonPressed:(id) sender
{
	@try
	{
		MapViewController * mapper = [[MapViewController alloc] init];
		[mapper setCordsToShow:[_location myLocation]];
		
		
	//	NSString *latlong = @"-33.874559,151.219575";
	//	
	//	NSString *url = [NSString stringWithFormat: @"http://maps.google.com/maps?ll=%@",
	//					 [latlong stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
		
	//	[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
		
		
		[[_appDelegate NavigationController] pushViewController:mapper animated:true];
	}
	@catch (NSException* ex) 
	{
		UIAlertView *charAlert = [[UIAlertView alloc]
								  initWithTitle:@"An Error Occuredname"
								  message:[ex reason]
								  delegate:nil
								  cancelButtonTitle:@"Done"
								  otherButtonTitles:nil];
		
		[charAlert show];
		[charAlert autorelease];
	}

}

- (IBAction)saveImageButton_pressed:(id)sender
{
	RealatorViewAppDelegate *appDelegate = (RealatorViewAppDelegate *)[[UIApplication sharedApplication] delegate];	
	@try
	{
		if([_location LocationID] == 0)
		{//Add Item
			DataAccess *a = [[DataAccess alloc] init];
			Location* newLocation = [self GetDisplayedLocation];
			[a AddLocation: newLocation];
		}
		else
		{
			DataAccess *a = [[DataAccess alloc] init];
			Location * newLocation = [self GetDisplayedLocation];
			[a UpdateLocation:newLocation];
		}
		[[appDelegate NavigationController] popViewControllerAnimated:true];
		[self release];
	}
	@catch (NSException* ex) 
	{
		UIAlertView *charAlert = [[UIAlertView alloc]
								  initWithTitle:@"An Error Occuredname"
								  message:[ex reason]
								  delegate:nil
								  cancelButtonTitle:@"Done"
								  otherButtonTitles:nil];
		
		[charAlert show];
		[charAlert autorelease];
	}
	
	
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}

- (void)dealloc {
    [super dealloc];
}


@end
